package admin.controllers;

import helpers.CookieHelper;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entities.Link;

import admin.models.LinkModel;

@SuppressWarnings("serial")
public class LinkDeleteServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		response.setContentType("text/html");
		String path = ((HttpServletRequest) request).getRequestURI();
		String id = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		try {
			LinkModel linkModel = LinkModel.getInstance();
			Link link = linkModel.get(id);
			if (link != null) {
				linkModel.delete(link);
			}
			CookieHelper.setCookie(response, "success", "Delete success link " + link.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		response.sendRedirect("/admin/link/list");
	}
}
